package com.woniuxy.medicine.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.entity.Medicine;
import com.woniuxy.commons.entity.QueryEntity;
import com.woniuxy.commons.utils.ResStatus;
import com.woniuxy.commons.utils.ResponseResult;
import com.woniuxy.medicine.dao.MedicineDao;
import com.woniuxy.medicine.service.MedicineService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 雷珉
 * @date 2022年06月08日 16:30
 */
@Service
public class MedicineServiceImpl implements MedicineService {
    @Resource
    private MedicineDao MedicineDao;

    @Override
    public ResponseResult<List<Medicine>> findAll() {
        List<Medicine> all = MedicineDao.findAll();
        return new ResponseResult<>(200,"查询成功",all,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult<PageInfo<Medicine>> findAllByCondition(QueryEntity<Medicine> queryEntity) {
        //开启分页
        PageHelper.startPage(queryEntity.getCurrentPage(),queryEntity.getPageSize());
        //排序
        String orderSort = queryEntity.getOrderSort();
        if("asc".equalsIgnoreCase(orderSort) || "desc".equalsIgnoreCase(orderSort)){
            PageHelper.orderBy(queryEntity.getOrderKey()+" "+orderSort);
        }
        //封装数据
        List<Medicine> all = MedicineDao.findAllByCondition(queryEntity.getEntity());
        PageInfo<Medicine> pageInfo = PageInfo.of(all);
        return new ResponseResult<>(200,"查询成功",pageInfo,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult<Medicine> findOne(int id) {
        Medicine one = MedicineDao.findOne(id);
        return new ResponseResult<>(200,"查询成功",one,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult<List<Medicine>> findBatch(int[] ids) {
        List<Medicine> all = MedicineDao.findBatch(ids);
        return new ResponseResult<>(200,"查询成功",all,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult addOne(Medicine Medicine) {
        MedicineDao.addOne(Medicine);
        return new ResponseResult<>(200,"添加成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult deleteOne(int id) {
        MedicineDao.deleteOne(id);
        return new ResponseResult<>(200,"删除成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult deleteBatch(int[] ids) {
        //删除发票
        MedicineDao.deleteBatch(ids);

        return new ResponseResult<>(200,"删除成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult updateOne(Medicine Medicine) {
        MedicineDao.updateOne(Medicine);
        return new ResponseResult<>(200,"更新成功",null,ResStatus.SUCCESS);
    }
}

